package com.eric.nioclient;

import android.app.Application;

import com.eric.nioclient.service.Logic;
import com.eric.nioclient.service.Service;

/**
 * Created by eric on 2017/11/17.
 */

public class App extends Application implements Logic.ServiceListener{

    Service service;
    boolean serviceIsStart = false;
    Logic logic = Logic.me();

    private static App app ;
    public static final App me(){
        return app;
    }

    @Override
    public void onCreate() {
        app = this;
        super.onCreate();
        logic.addListener(this);

    }

    public boolean serviceAvailable(){
        return serviceIsStart;
    }

    public void startService(String host,int port){
        if(service != null){
            service.stopService();
        }
        service = new Service(host,port);
        service.startService();
    }

    public void stopService(){
        if(service != null){
            service.stopService();
        }
    }

    @Override
    public void started() {
        serviceIsStart = true;
    }

    @Override
    public void stoped(Object obj) {
        serviceIsStart = true;
    }

    @Override
    public void receivedMsg(String sendUser, String msg) {

    }

    @Override
    public void receivedNotice(String msg) {

    }

    @Override
    public void loginSuccess() {
        serviceIsStart = true;
    }

    @Override
    public void loginFail(String msg) {
        serviceIsStart = true;
    }

    @Override
    public void connectActive() {

    }
}
